#include "widget.h"
#include <QDebug>

Widget::Widget()
{
    qmlRegisterType<PipeWireSourceItem>("org.kde.kpipewire", 1, 0, "PipeWireSourceItem");
    qmlRegisterType<ScreencastingRequest>("org.kde.kpipewire", 1, 0, "ScreencastingRequest");

    rootContext()->setContextProperty("widget", this);
    setSource(QUrl("qrc:/main.qml"));

    m_connection = KWayland::Client::ConnectionThread::fromApplication(this);

    if (!m_connection) {
        return;
    }

    m_registry = new KWayland::Client::Registry(this);

    m_registry->create(m_connection);

    QObject::connect(m_registry, &KWayland::Client::Registry::plasmaWindowManagementAnnounced, [this](quint32 name, quint32 version) {
        m_windowManagement = m_registry->createPlasmaWindowManagement(name, version, this);
        connect(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated, this, [this](KWayland::Client::PlasmaWindow *w) {
            qDebug() << "Window uuid: " << w->uuid() << " title: " << w->title();
            m_uuidList.append(w->uuid());
            connect(w, &KWayland::Client::PlasmaWindow::titleChanged, this, [w] { qDebug() << "Window title changed to: " << w->title(); });
        });
    });

    m_registry->setup();
    m_connection->roundtrip();
}

Widget::~Widget()
{
}

void Widget::slotAnyway()
{
    qDebug() << "Widget::slotAnyway()";
}